/**
 * Created by wangp on 2016/8/22.
 */
$(function(){
    //注册验证
    function validate(){
        var validate = $('#sign-form').validate();
        var ck = validate.checkForm();
        if( validate.check($('input[name="mobile"]')) && !$('.getcode').hasClass('disabled') ){
            $('.getcode').removeAttr('disabled');
        }else{
            $('.getcode').attr('disabled', true);
        }
        if( ck ){
            $('.sign-btn').removeAttr('disabled');
        }else {
            $('.sign-btn').attr('disabled', true);
        }
    }
    //删除按钮tap事件，验证form表单
    $('#sign-form').on('click','.mui-icon-clear',function(){
        validate();
    });
    //输入框keyup事件，验证form表单
    $('#sign-form .mui-input-row .mui-input').on('keyup', function(){
        validate();
    });
    //发送短信
    $('.getcode').on('click',function(){
        if( $(this).hasClass('fpwd') )  return false;
        if( $(this).hasClass('zycode') ) {
            send_code($(this),'zycode');
        }
		else send_code($(this),'loginout');
    });

    //注册点击事件
    $('#btn-signup').click(function(){
        var mobile = $('#sign-form input[name="mobile"]').val(),
            code = $('#sign-form input[name="code"]').val(),
            password = $('#sign-form input[name="password"]').val();
            //opid = getUrlParam('opid'),
            //token = getUrlParam('token'),
            //type = getUrlParam('type');
        var data = {
            apiCode: '/Public/register',
            phone: mobile,
            code: code,
            password: password,
            role_id: 1
        };

        console.log(data);
        http(data, function(responed){
            if( responed.success == 1 ){
                $.cookie('userInfo', JSON.stringify(responed.data));
                mui.alert(responed.msg);
                setTimeout(function(){
                        location.href = 'signin.html';
                }, 1000);
            }else{
                mui.alert(responed.msg);
            }
        });
    });
    //评委注册点击
    $('#pingweizhuce').on('click',function(){
        var judge_name = $('#pingwei-sign input[name="name"]').val();
        var sex = $('#psex').val();
        var job = $('#pingwei-sign input[name="zhiye"]').val();
        var address = $('#pingwei-sign input[name="dizhi"]').val();
        var zizhi = $('#pid').val();
        var zizhi_id =$('#pingwei-sign input[name="zizhi"]').val();
        var judge_face = '';
        var judge_desc =$('.pingwei-lvli').text();
        var other_desc=$('.pingwei-qita').text();
        if(!judge_name){
            mui.alert('姓名必须填写');
            return false;
        }
        if(!job){
            mui.alert('工作必须填写');
            return false;
        }
        if(!address){
            mui.alert('地址必须填写');
            return false;
        }
        if(!zizhi_id){
            mui.alert('资质必须填写');
            return false;
        }
        if(!judge_face){
            mui.alert('相片必须上传');
            return false;
        }
        var data = {
            apiCode :'/User/editUserInfo',
            judge_name: judge_name,
            sex: sex,
            job:job,
            address:address,
            zizhi:zizhi,
        };
        http(data, function(responed){
            if( responed.success == 1 ){
                mui.toast(responed.msg, 1000);
                $.cookie('userInfo', JSON.stringify(responed.info));
                setTimeout(function(){
                    if(returnurl !=undefined&&returnurl !=""){
                        location.href = returnurl;
                    }else{
                        location.href = '../index/index.html';
                    }
                }, 1000);
            }else{
                mui.alert(responed.info);
            }
        });
    })

    //登录点击事件
    $('#btn-signin').on('click',function(){
        var mobile = $('#sign-form input[name="mobile"]').val(),
            password = $('#sign-form input[name="password"]').val();
        var data = {
            apiCode :'/Public/login',
            phone: mobile,
            password: password,
            role_id:1
        };
        http(data, function(responed){
            console.log(responed);
            if( responed.success == 1 ){
                mui.toast(responed.msg);
                $.cookie('userInfo',JSON.stringify(responed.data));
                console.log( $.cookie('userInfo'));
                    setTimeout(function(){
                            location.href = '../index/saishi_detail.html?match_id=5';
                    }, 1000);
            }else{
                mui.alert(responed.msg);
                return false;
            }
        });
    });
    //忘记密码点击事件
    $('#forgetPwd').click(function(){
        var mobile = $('#sign-form input[name="mobile"]').val(),
            code = $('#sign-form input[name="code"]').val(),
            newPwd = $('#sign-form input[name="newPwd"]').val(),
            sureNewPwd = $('#sign-form input[name="sureNewPwd"]').val();
        if(newPwd!=sureNewPwd){
            mui.toast('密码错误', 1000);
        }
        var data = {
            apiCode: '_set_pwd_003',
            mobile: mobile,
            code: code,
            loginPass: newPwd,
            sureNewPwd: sureNewPwd
        };
        http(data, function(responed){
            if( responed.error == 0 ){
                mui.toast('密码设置成功', 1000);
                setTimeout(function(){
                    location.href = 'signin.html';
                }, 1000);
            }else{
                mui.alert(responed.info);
            }
        });
    });
});
function send_code(self, tp, opt){
    var mobile = $('#sign-form input[name="mobile"]');
    if( !mobile.val() ){
        mui.alert('请输入手机号码！');
        mobile.focus();
        return false;
    }
    var data = {
        apiCode: '/Public/sendCode',
        phone: mobile.val()
    };
    http(data, function(responed){
        if( responed.success == 1 ){
            mui.toast('发送成功');
            var ms = 59;
            self.attr('disabled', true).addClass('disabled').text('还剩60秒');
            //创建倒计时
            var interval = setInterval(function(){
                self.text('还剩'+(ms--)+'秒');
            }, 1000);
            //60秒后清楚计时器
            setTimeout(function(){
                clearInterval(interval);
                self.removeAttr('disabled').removeClass('disabled').text('获取验证码');
                return true;
            }, 60000)
        }else{
            mui.alert(responed.info);
        }
    });
};
